import entity.Node;
import entity.NodeType;
import entity.Relation;
import entity.RelationshipType;
import org.neo4j.driver.*;
import org.neo4j.driver.Record;
import org.neo4j.driver.internal.InternalNode;
import org.neo4j.driver.internal.value.NodeValue;
import service.NodeService;
import service.RelationService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

public class Neo4jHelper {
    public Driver driver;
    public Session session;
    public NodeService nodeService;
    public RelationService relationService;

    public Neo4jHelper(String url, String username, String password, String database) {
        this.driver = GraphDatabase.driver(url, AuthTokens.basic(username, password));
        this.session = driver.session(SessionConfig.forDatabase(database));
        this.nodeService = new NodeService(session);
        this.relationService = new RelationService(session);
    }

    public Neo4jHelper() {
        this.driver = GraphDatabase.driver("bolt://localhost:11011", AuthTokens.basic("neo4j", "neo4j2"));
        this.session = driver.session(SessionConfig.forDatabase("fsm"));
        this.nodeService = new NodeService(session);
        this.relationService = new RelationService(session);
    }

    private void testConnection() {
        try (Session session = driver.session()) {
            Result result = session.run("MATCH (n1)-[r]->(n2) RETURN r, n1, n2 LIMIT 25");
            System.out.println(result);
            while (result.hasNext()) {
                Record record = result.next();
                System.out.println(record.fields());
                System.out.println(record.get("id").asString() + " " + record.get("id").asString());
            }
        }
        driver.close();
    }

    public void close() {
        this.session.close();
    }

    public static void main(String[] args) {
        Neo4jHelper neo4jHelper = new Neo4jHelper();
        neo4jHelper.nodeService.getNodes();
        for (Relation relation : neo4jHelper.relationService.getRelation()) {
            System.out.println(relation);
        }

        neo4jHelper.close();
    }

}
